package com.qianxx.base.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.ImageView;

import com.qianxx.draw.R;
import com.lidroid.xutils.BitmapUtils;

import java.io.File;
import java.io.InputStream;

/**
 * 在Xutils的图片下载方法上封装的单例模式
 * 使用的时候直接调MyBitmapUtil.getInstance(context).setBitmap(imageView,URL);
 */
public class MyBitmapUtil {
    private MyBitmapUtil() {
    }

    private static MyBitmapUtil MyBitmapUtil;
    private File file;
    private static BitmapUtils bitmapUtils;// 这是xutils的图片下载类对象
    private static Context mContext;

    public static MyBitmapUtil getInstance(Context context) {

        if (MyBitmapUtil == null) {
            synchronized (MyBitmapUtil.class) {
                if (MyBitmapUtil == null) {
                    MyBitmapUtil = new MyBitmapUtil();
                    bitmapUtils = new BitmapUtils(context,
                            Environment.getExternalStorageDirectory()
                                    + "/LeaseBitmapCache/");
                    bitmapUtils.configThreadPoolSize(5);


                    bitmapUtils
                            .configDefaultBitmapConfig(Bitmap.Config.RGB_565);
                    bitmapUtils.configDiskCacheEnabled(true);
                    bitmapUtils.configMemoryCacheEnabled(true);
                }
            }
        }
        mContext = context;

        return MyBitmapUtil;
    }

    private static Bitmap defaultIcon; // 默认的订单头像图标

    // 优先加载sd卡里下载过的图片，没有就网络加载
    public void setBitmap(ImageView imageView, String Url) {
        if (errorImgId == 0) {
            bitmapUtils.configDefaultLoadingImage(R.drawable.icon_pic_big);// 下载中显示的图片
            bitmapUtils.configDefaultLoadFailedImage(R.drawable.icon_pic_big);// 下载失败后显示的图片
            // 默认的bitmap图片
            InputStream isHos = mContext.getResources().openRawResource(R.raw.loading_img);
            defaultIcon = BitmapFactory.decodeStream(isHos);
        } else {
            bitmapUtils.configDefaultLoadingImage(errorImgId);// 下载中显示的图片
            bitmapUtils.configDefaultLoadFailedImage(errorImgId);// 下载失败后显示的图片
            // 默认的bitmap图片
            InputStream isHos = mContext.getResources().openRawResource(errorImgId);
            defaultIcon = BitmapFactory.decodeStream(isHos);
        }


        // Url =
        // "http://120.25.219.103:8180/images/UI00000103/myPic/thum/20150711b0cfa32b-0b87-4905-bdf9-0f6f20516c11.jpg";
        imageView.setImageBitmap(defaultIcon);
        setBitmap2(imageView, Url);

    }

    private int errorImgId = 0;

    public int getErrorImgId() {
        return errorImgId;
    }

    public void setErrorImgId(int errorImgId) {
        this.errorImgId = errorImgId;
    }

    public void setBitmap2(ImageView imageView, String Url) {
        if (!TextUtils.isEmpty(Url)) {
            file = bitmapUtils.getBitmapFileFromDiskCache(Url);
            if (file != null) {
                bitmapUtils.display(imageView, file.toString());
            } else {
                try {
                    bitmapUtils.display(imageView, Url);
                } catch (Exception e) {
                    if (errorImgId == 0) {
                        imageView.setImageResource(R.drawable.icon_pic_big);
                    } else {
                        imageView.setImageResource(errorImgId);
                    }

                }

            }
        } else {
            if (errorImgId == 0) {
                imageView.setImageResource(R.drawable.icon_pic_big);
            } else {
                imageView.setImageResource(errorImgId);
            }
        }

    }

    /**
     * 获取网络图片加载到本地的图片路径
     */
    public String getBitmapCachePath(String Url) {
        File loaded = isLoaded(Url);
        if (loaded != null) {
            return loaded.getAbsolutePath();
        } else {
            return null;
        }
    }

    /**
     * 判断网络图片是否被加载
     *
     * @param Url 网络图片的URL
     * @return File加载过的本地图片文件， null 表示没被加载
     */
    public File isLoaded(String Url) {
        return bitmapUtils.getBitmapFileFromDiskCache(Url);
    }
}
